<?php
require_once 'models/TCMSPageCategoryModel.inc';
require_once 'models/TCMSCategoryModel.inc';
require_once 'pages/TMSItemPage.inc';
require_once 'widgets/TSkinnableWidget.inc';

class TPageCategoriesWidget extends TSkinnableWidget {
/**
 * @var TCMSCategoryModel
 */	
	protected $_ioc_categories_;
	
	protected function checkSource($src){
		return $src instanceof TCMSPageCategoryModel;
	}	
	
	public function http_MovePage($categories = array()){
		$this->Source->MovePage($this->response->ModelItem->Base,array_keys($categories));
	}
	
	public function Css(){
		?>
			div.page-categories {
				border-style:inset;
				border-width:1px;
				padding-left:15px;
			}
			
			div.page-categories div.tree-node-subs {
				padding-left:20px;
			}
			
			div.page-categories div.tree-node-title {
				border-bottom-style:dotted;
				border-bottom-width:1px;
				border-bottom-color:gray;
				padding:4px;
			}
			
			div.page-categories button {
				font-size:12pt;
				padding:5px;
				margin-top:5px;
				margin-bottom:5px;
			}
			
			div.page-categories form div.tree {
				height:300px;
				overflow:scroll;
			}
		<?php
	}
	
	private function _render_node(TCandyItem $node, array $categories){
		?>
		<div class="tree-node">
			<div class="tree-node-title"><input type="checkbox" name="categories[<?php echo $node->Base->Id; ?>]" <?php if (in_array($node->Base->Id,$categories)) { ?> checked="true"<?php } ?>><?php echo $node; ?></div>
			<div class="tree-node-subs">
			<?php
				$c = $node->Contents();
				foreach ($c as $subnode)
					$this->_render_node($subnode,$categories); 
			?>
			</div>
		</div>
		<?php
	}
	
	public function defaultOutput(IThemeEngine $engine){
		?>
		<div id="<?php echo $this->Name(); ?>" class="page-categories">
		<form method="post">
			<input type="hidden" name="controller" value="<?php echo $this->Name(); ?>" />
			<input type="hidden" name="method" value="MovePage" />
			<div class="tree">
			<?php 
				$categories = array(); 
				foreach ($this->response->ModelItem->Categories as $cat)
					$categories[] = $cat->Id;
				$cat = $this->Categories->GetContents(null,false);
				foreach ($cat as $node)
					$this->_render_node($node,$categories);
			?>
			</div>
			<button onclick="javascript:submit();"><?php __i('Move'); ?></button>
		</form>
		</div>
		<?php 
	}
}
